<template>
  <main class="setting-page">
    <div class="form-title">
      <p class="title">认证管理</p>
      <a-button type="primary" class="common-btn" preIcon="ant-design:plus-outlined" @click="handleAdd">新增</a-button>
    </div>
    <section class="card-list">
      <section class="card" v-for="(item, index) in authManages" :key="index">
        <div class="card-head">
          <img class="card-image" :src="p.img" v-for="(p, idx) in authType" :key="idx" v-show="item.type == p.name" />
          <p class="card-name text-ellipsis">
            {{ item.name }}
            <Icon class="card-icon-check" icon="ant-design:check-circle-outlined" v-if="item.state" />
            <Icon class="card-icon-close" icon="ant-design:close-circle-outlined" v-else />
          </p>
          <div class="btns">
            <img
              class="row-btn-img"
              :src="i.img"
              v-for="(i, idx) in actionBtns"
              :key="idx"
              @click="headerBtnItemHandle(item, i.click)"
              v-show="i.click.defaultshow || i.click.isshow == item.enable"
            />
          </div>
        </div>
        <p class="card-row"> 类型：{{ item.type }} </p>
        <p class="card-row text-ellipsis">
          <a-tooltip placement="top">
            <template #title>{{ item.filter ? item.filter : '无' }}</template>
            匹配规则： {{ item.filter ? item.filter : '无' }}
          </a-tooltip>
        </p>
        <p class="card-row"> {{ item.createTime }} </p>
      </section>
    </section>
    <AuthDrawer @register="registerDrawer" @success="handleSuccess" />
    <a-modal title="提示" v-model:open="runVisible" centered @ok="handleRunOk">
      <p class="modal-content">确定{{ authItemInfo.info.enable ? '停止' : '启动' }}该认证吗？</p>
    </a-modal>
    <a-modal title="提示" v-model:open="deleteVisible" centered @ok="handleDeleteOk">
      <p class="modal-content">确定删除该认证吗？</p>
    </a-modal>
  </main>
</template>
<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import AuthDrawer from './AuthDrawer.vue';
  import authManages from './auth.json';
  import { actionBtns, authType } from './auth.data';
  import { runRequest, deleteRequest } from './auth.api';
  import { useDrawer } from '/@/components/Drawer';
  const [registerDrawer, { openDrawer }] = useDrawer();
  const authItemInfo = reactive({ info: {} });
  const runVisible = ref<boolean>(false);
  const deleteVisible = ref<boolean>(false);
  function handleAdd() {
    openDrawer(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  function handleEdit(record) {
    openDrawer(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }
  function handleDetail(record) {
    openDrawer(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }
  function handleRun(record) {
    authItemInfo.info = record;
    runVisible.value = true;
  }
  async function handleRunOk() {
    authItemInfo.info.enable = !authItemInfo.info.enable;
    await runRequest(authItemInfo.info);
  }
  function handleDelete(record) {
    authItemInfo.info = record;
    deleteVisible.value = true;
  }
  async function handleDeleteOk() {
    await deleteRequest(authItemInfo.info);
  }
  function headerBtnItemHandle(item, i) {
    headerBtnHandle(item, i.name);
  }
  function headerBtnHandle(item, methodName) {
    const actions = {
      handleRun,
      handleDelete,
      handleEdit,
      handleDetail,
    };
    const action = actions[methodName as keyof typeof actions];
    action && action(item);
  }
</script>
<style lang="less" scoped>
  p {
    margin-bottom: 0;
  }
  .setting-page {
    padding: 20px;
    background-color: #fff;
    box-shadow: 0 0 18px 0 rgba(0, 0, 0, 0.06);
    border-radius: 8px;
  }
  .setting-page .form-title {
    display: flex;
    margin: 16px 0 8px;
  }
  .form-title .title {
    font-size: 15px;
    text-align: left;
    height: 34px;
    line-height: 34px;
    font-weight: 600;
    color: #0b1e42;
  }
  .common-btn {
    background: linear-gradient(144deg, rgba(18, 76, 255, 0.12), rgba(67, 147, 255, 0.12));
    border-radius: 18px;
    font-size: 13px;
    color: #1d6bff;
    margin: 0 16px;
  }
  .setting-page .card-list {
    display: flex;
    flex-wrap: wrap;
  }
  .setting-page .card-list .card {
    flex: 0 0 390px;
    height: 180px;
    overflow: hidden;
    background: #f5f8fa;
    border-radius: 8px;
    padding: 16px;
    margin: 0 16px 16px 0;
  }
  .card-list .card .card-head {
    display: flex;
    justify-content: space-between;
    margin-bottom: 16px;
  }
  .card-list .card .card-row {
    font-size: 13px;
    color: #788aa0;
    line-height: 18px;
    margin-top: 6px;
  }
  .card-list .card .card-head .btns {
    flex: 0 0 180px;
    text-align: right;
  }
  .card-list .card .card-head .card-image {
    width: 54px;
    height: 54px;
    border-radius: 4px;
  }
  .card-list .card .card-head .card-name {
    flex: 1;
    line-height: 67px;
    padding: 0 16px;
    font-size: 15px;
    font-weight: 600;
    color: #0b1e42;
  }
  .card-list .card .card-head .card-icon-close {
    color: green;
  }
  .card-list .card .card-head .card-icon-check {
    color: red;
  }
  .text-ellipsis {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .card-list .card .card-head .btns .row-btn-img {
    width: 24px;
    height: 24px;
    cursor: pointer;
    margin: 21px 10px 0 2px;
  }
  .modal-content {
    height: 80px;
    line-height: 80px;
    padding-left: 30px;
    font-size: 15px;
  }
</style>
